<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Reverse Iterator</title>
<link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../index.html" title="Chapter 1. Boost.Iterator">
<link rel="up" href="../specialized.html" title="Specialized Adaptors">
<link rel="prev" href="permutation.html" title="Permutation Iterator">
<link rel="next" href="shared_container.html" title="Shared Container Iterator">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="permutation.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../specialized.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="shared_container.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="iterator.specialized.reverse"></a><a class="link" href="reverse.html" title="Reverse Iterator">Reverse Iterator</a>
</h3></div></div></div>
<p>
        The reverse iterator adaptor iterates through the adapted iterator range
        in the opposite direction.
      </p>
<h3>
<a name="iterator.specialized.reverse.h0"></a>
        <span class="phrase"><a name="iterator.specialized.reverse.example"></a></span><a class="link" href="reverse.html#iterator.specialized.reverse.example">Example</a>
      </h3>
<p>
        The following example prints an array of characters in reverse order using
        <code class="computeroutput"><span class="identifier">reverse_iterator</span></code>.
      </p>
<pre class="programlisting"><span class="keyword">char</span> <span class="identifier">letters_</span><span class="special">[]</span> <span class="special">=</span> <span class="string">"hello world!"</span><span class="special">;</span>
<span class="keyword">const</span> <span class="keyword">int</span> <span class="identifier">N</span> <span class="special">=</span> <span class="keyword">sizeof</span><span class="special">(</span><span class="identifier">letters_</span><span class="special">)/</span><span class="keyword">sizeof</span><span class="special">(</span><span class="keyword">char</span><span class="special">)</span> <span class="special">-</span> <span class="number">1</span><span class="special">;</span>
<span class="keyword">typedef</span> <span class="keyword">char</span><span class="special">*</span> <span class="identifier">base_iterator</span><span class="special">;</span>
<span class="identifier">base_iterator</span> <span class="identifier">letters</span><span class="special">(</span><span class="identifier">letters_</span><span class="special">);</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"original sequence of letters:\t\t\t"</span> <span class="special">&lt;&lt;</span> <span class="identifier">letters_</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>

<span class="identifier">boost</span><span class="special">::</span><span class="identifier">reverse_iterator</span><span class="special">&lt;</span><span class="identifier">base_iterator</span><span class="special">&gt;</span>
  <span class="identifier">reverse_letters_first</span><span class="special">(</span><span class="identifier">letters</span> <span class="special">+</span> <span class="identifier">N</span><span class="special">),</span>
  <span class="identifier">reverse_letters_last</span><span class="special">(</span><span class="identifier">letters</span><span class="special">);</span>

<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"sequence in reverse order:\t\t\t"</span><span class="special">;</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">copy</span><span class="special">(</span><span class="identifier">reverse_letters_first</span><span class="special">,</span> <span class="identifier">reverse_letters_last</span><span class="special">,</span>
          <span class="identifier">std</span><span class="special">::</span><span class="identifier">ostream_iterator</span><span class="special">&lt;</span><span class="keyword">char</span><span class="special">&gt;(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span><span class="special">));</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>

<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"sequence in double-reversed (normal) order:\t"</span><span class="special">;</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">copy</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">make_reverse_iterator</span><span class="special">(</span><span class="identifier">reverse_letters_last</span><span class="special">),</span>
          <span class="identifier">boost</span><span class="special">::</span><span class="identifier">make_reverse_iterator</span><span class="special">(</span><span class="identifier">reverse_letters_first</span><span class="special">),</span>
          <span class="identifier">std</span><span class="special">::</span><span class="identifier">ostream_iterator</span><span class="special">&lt;</span><span class="keyword">char</span><span class="special">&gt;(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span><span class="special">));</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
</pre>
<p>
        The output is:
      </p>
<pre class="programlisting">original sequence of letters:                   hello world!
sequence in reverse order:                      !dlrow olleh
sequence in double-reversed (normal) order:     hello world!
</pre>
<p>
        The source code for this example can be found <a href="../../../../example/reverse_iterator_example.cpp" target="_top">here</a>.
      </p>
<h3>
<a name="iterator.specialized.reverse.h1"></a>
        <span class="phrase"><a name="iterator.specialized.reverse.reference"></a></span><a class="link" href="reverse.html#iterator.specialized.reverse.reference">Reference</a>
      </h3>
<h4>
<a name="iterator.specialized.reverse.h2"></a>
        <span class="phrase"><a name="iterator.specialized.reverse.synopsis"></a></span><a class="link" href="reverse.html#iterator.specialized.reverse.synopsis">Synopsis</a>
      </h4>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">Iterator</span><span class="special">&gt;</span>
<span class="keyword">class</span> <span class="identifier">reverse_iterator</span>
<span class="special">{</span>
<span class="keyword">public</span><span class="special">:</span>
  <span class="keyword">typedef</span> <span class="identifier">iterator_traits</span><span class="special">&lt;</span><span class="identifier">Iterator</span><span class="special">&gt;::</span><span class="identifier">value_type</span> <span class="identifier">value_type</span><span class="special">;</span>
  <span class="keyword">typedef</span> <span class="identifier">iterator_traits</span><span class="special">&lt;</span><span class="identifier">Iterator</span><span class="special">&gt;::</span><span class="identifier">reference</span> <span class="identifier">reference</span><span class="special">;</span>
  <span class="keyword">typedef</span> <span class="identifier">iterator_traits</span><span class="special">&lt;</span><span class="identifier">Iterator</span><span class="special">&gt;::</span><span class="identifier">pointer</span> <span class="identifier">pointer</span><span class="special">;</span>
  <span class="keyword">typedef</span> <span class="identifier">iterator_traits</span><span class="special">&lt;</span><span class="identifier">Iterator</span><span class="special">&gt;::</span><span class="identifier">difference_type</span> <span class="identifier">difference_type</span><span class="special">;</span>
  <span class="keyword">typedef</span> <span class="comment">/* see below */</span> <span class="identifier">iterator_category</span><span class="special">;</span>

  <span class="identifier">reverse_iterator</span><span class="special">()</span> <span class="special">{}</span>
  <span class="keyword">explicit</span> <span class="identifier">reverse_iterator</span><span class="special">(</span><span class="identifier">Iterator</span> <span class="identifier">x</span><span class="special">)</span> <span class="special">;</span>

  <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">OtherIterator</span><span class="special">&gt;</span>
  <span class="identifier">reverse_iterator</span><span class="special">(</span>
      <span class="identifier">reverse_iterator</span><span class="special">&lt;</span><span class="identifier">OtherIterator</span><span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">r</span>
    <span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">enable_if_convertible</span><span class="special">&lt;</span><span class="identifier">OtherIterator</span><span class="special">,</span> <span class="identifier">Iterator</span><span class="special">&gt;::</span><span class="identifier">type</span><span class="special">*</span> <span class="special">=</span> <span class="number">0</span> <span class="comment">// exposition</span>
  <span class="special">);</span>
  <span class="identifier">Iterator</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">base</span><span class="special">()</span> <span class="keyword">const</span><span class="special">;</span>
  <span class="identifier">reference</span> <span class="keyword">operator</span><span class="special">*()</span> <span class="keyword">const</span><span class="special">;</span>
  <span class="identifier">reverse_iterator</span><span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">++();</span>
  <span class="identifier">reverse_iterator</span><span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">--();</span>
<span class="keyword">private</span><span class="special">:</span>
  <span class="identifier">Iterator</span> <span class="identifier">m_iterator</span><span class="special">;</span> <span class="comment">// exposition</span>
<span class="special">};</span>
</pre>
<p>
        If <code class="computeroutput"><span class="identifier">Iterator</span></code> models Random
        Access Traversal Iterator and Readable Lvalue Iterator, then <code class="computeroutput"><span class="identifier">iterator_category</span></code> is convertible to <code class="computeroutput"><span class="identifier">random_access_iterator_tag</span></code>. Otherwise,
        if <code class="computeroutput"><span class="identifier">Iterator</span></code> models Bidirectional
        Traversal Iterator and Readable Lvalue Iterator, then <code class="computeroutput"><span class="identifier">iterator_category</span></code>
        is convertible to <code class="computeroutput"><span class="identifier">bidirectional_iterator_tag</span></code>.
        Otherwise, <code class="computeroutput"><span class="identifier">iterator_category</span></code>
        is convertible to <code class="computeroutput"><span class="identifier">input_iterator_tag</span></code>.
      </p>
<h4>
<a name="iterator.specialized.reverse.h3"></a>
        <span class="phrase"><a name="iterator.specialized.reverse.requirements"></a></span><a class="link" href="reverse.html#iterator.specialized.reverse.requirements">Requirements</a>
      </h4>
<p>
        <code class="computeroutput"><span class="identifier">Iterator</span></code> must be a model
        of Bidirectional Traversal Iterator. The type <code class="computeroutput"><span class="identifier">iterator_traits</span><span class="special">&lt;</span><span class="identifier">Iterator</span><span class="special">&gt;::</span><span class="identifier">reference</span></code>
        must be the type of <code class="computeroutput"><span class="special">*</span><span class="identifier">i</span></code>,
        where <code class="computeroutput"><span class="identifier">i</span></code> is an object of type
        <code class="computeroutput"><span class="identifier">Iterator</span></code>.
      </p>
<h4>
<a name="iterator.specialized.reverse.h4"></a>
        <span class="phrase"><a name="iterator.specialized.reverse.concepts"></a></span><a class="link" href="reverse.html#iterator.specialized.reverse.concepts">Concepts</a>
      </h4>
<p>
        A specialization of <code class="computeroutput"><span class="identifier">reverse_iterator</span></code>
        models the same iterator traversal and iterator access concepts modeled by
        its <code class="computeroutput"><span class="identifier">Iterator</span></code> argument. In
        addition, it may model old iterator concepts specified in the following table:
      </p>
<div class="table">
<a name="iterator.specialized.reverse.categories"></a><p class="title"><b>Table 1.15. Categories</b></p>
<div class="table-contents"><table class="table" summary="Categories">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>
                <p>
                  If <code class="computeroutput"><span class="identifier">I</span></code> models
                </p>
              </th>
<th>
                <p>
                  then <code class="computeroutput"><span class="identifier">reverse_iterator</span><span class="special">&lt;</span><span class="identifier">I</span><span class="special">&gt;</span></code> models
                </p>
              </th>
</tr></thead>
<tbody>
<tr>
<td>
                <p>
                  Readable Lvalue Iterator, Bidirectional Traversal Iterator
                </p>
              </td>
<td>
                <p>
                  Bidirectional Iterator
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  Writable Lvalue Iterator, Bidirectional Traversal Iterator
                </p>
              </td>
<td>
                <p>
                  Mutable Bidirectional Iterator
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  Readable Lvalue Iterator, Random Access Traversal Iterator
                </p>
              </td>
<td>
                <p>
                  Random Access Iterator
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  Writable Lvalue Iterator, Random Access Traversal Iterator
                </p>
              </td>
<td>
                <p>
                  Mutable Random Access Iterator
                </p>
              </td>
</tr>
</tbody>
</table></div>
</div>
<br class="table-break"><p>
        <code class="computeroutput"><span class="identifier">reverse_iterator</span><span class="special">&lt;</span><span class="identifier">X</span><span class="special">&gt;</span></code> is
        interoperable with <code class="computeroutput"><span class="identifier">reverse_iterator</span><span class="special">&lt;</span><span class="identifier">Y</span><span class="special">&gt;</span></code>
        if and only if <code class="computeroutput"><span class="identifier">X</span></code> is interoperable
        with <code class="computeroutput"><span class="identifier">Y</span></code>.
      </p>
<h4>
<a name="iterator.specialized.reverse.h5"></a>
        <span class="phrase"><a name="iterator.specialized.reverse.operations"></a></span><a class="link" href="reverse.html#iterator.specialized.reverse.operations">Operations</a>
      </h4>
<p>
        In addition to the operations required by the concepts modeled by <code class="computeroutput"><span class="identifier">reverse_iterator</span></code>, <code class="computeroutput"><span class="identifier">reverse_iterator</span></code>
        provides the following operations.
      </p>
<pre class="programlisting"><span class="identifier">reverse_iterator</span><span class="special">();</span>
</pre>
<p>
        <span class="bold"><strong>Requires: </strong></span> <code class="computeroutput"><span class="identifier">Iterator</span></code>
        must be Default Constructible.<br> <span class="bold"><strong>Effects: </strong></span>
        Constructs an instance of <code class="computeroutput"><span class="identifier">reverse_iterator</span></code>
        with <code class="computeroutput"><span class="identifier">m_iterator</span></code> default constructed.
      </p>
<pre class="programlisting"><span class="keyword">explicit</span> <span class="identifier">reverse_iterator</span><span class="special">(</span><span class="identifier">Iterator</span> <span class="identifier">x</span><span class="special">);</span>
</pre>
<p>
        <span class="bold"><strong>Effects: </strong></span> Constructs an instance of <code class="computeroutput"><span class="identifier">reverse_iterator</span></code> with <code class="computeroutput"><span class="identifier">m_iterator</span></code>
        copy constructed from <code class="computeroutput"><span class="identifier">x</span></code>.
      </p>
<pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">OtherIterator</span><span class="special">&gt;</span>
<span class="identifier">reverse_iterator</span><span class="special">(</span>
    <span class="identifier">reverse_iterator</span><span class="special">&lt;</span><span class="identifier">OtherIterator</span><span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">r</span>
  <span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">enable_if_convertible</span><span class="special">&lt;</span><span class="identifier">OtherIterator</span><span class="special">,</span> <span class="identifier">Iterator</span><span class="special">&gt;::</span><span class="identifier">type</span><span class="special">*</span> <span class="special">=</span> <span class="number">0</span> <span class="comment">// exposition</span>
<span class="special">);</span>
</pre>
<p>
        <span class="bold"><strong>Requires: </strong></span> <code class="computeroutput"><span class="identifier">OtherIterator</span></code>
        is implicitly convertible to <code class="computeroutput"><span class="identifier">Iterator</span></code>.<br>
        <span class="bold"><strong>Effects: </strong></span> Constructs instance of <code class="computeroutput"><span class="identifier">reverse_iterator</span></code> whose <code class="computeroutput"><span class="identifier">m_iterator</span></code>
        subobject is constructed from <code class="computeroutput"><span class="identifier">y</span><span class="special">.</span><span class="identifier">base</span><span class="special">()</span></code>.
      </p>
<pre class="programlisting"><span class="identifier">Iterator</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">base</span><span class="special">()</span> <span class="keyword">const</span><span class="special">;</span>
</pre>
<p>
        <span class="bold"><strong>Returns: </strong></span> <code class="computeroutput"><span class="identifier">m_iterator</span></code>
      </p>
<pre class="programlisting"><span class="identifier">reference</span> <span class="keyword">operator</span><span class="special">*()</span> <span class="keyword">const</span><span class="special">;</span>
</pre>
<p>
        <span class="bold"><strong>Effects: </strong></span> Iterator tmp = m_iterator; return
        *--tmp;
      </p>
<pre class="programlisting"><span class="identifier">reverse_iterator</span><span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">++();</span>
</pre>
<p>
        <span class="bold"><strong>Effects: </strong></span> <code class="computeroutput"><span class="special">--</span><span class="identifier">m_iterator</span></code><br> <span class="bold"><strong>Returns:
        </strong></span> <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code>
      </p>
<pre class="programlisting"><span class="identifier">reverse_iterator</span><span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">--();</span>
</pre>
<p>
        <span class="bold"><strong>Effects: </strong></span> <code class="computeroutput"><span class="special">++</span><span class="identifier">m_iterator</span></code><br> <span class="bold"><strong>Returns:
        </strong></span> <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code>
      </p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2003, 2005 David Abrahams Jeremy Siek Thomas
      Witt<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at &lt;ulink url="http://www.boost.org/LICENSE_1_0.txt"&gt;
        http://www.boost.org/LICENSE_1_0.txt &lt;/ulink&gt;)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="permutation.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../specialized.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="shared_container.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
